package com.lenovo.function

import java.text.SimpleDateFormat
import java.util.{Calendar, Date}

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapred.TableOutputFormat
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.mapred.JobConf
import org.apache.spark.sql.SparkSession

/**
  * userprofile nature base label
  */
class Utils extends Serializable {


  @transient
  val driver = "com.mysql.jdbc.Driver"
  @transient
  val url  = "jdbc:mysql://10.122.23.164:3306/lenovo_upp"
  @transient
  val user= "upp"
  @transient
  val password = "Lenovo!23Upp"

//  @transient
//  val driver = "com.mysql.jdbc.Driver"
//  @transient
//  val url  = "jdbc:mysql://10.96.91.186:3306/lenovo_upp_test"
//  @transient
//  val user= "root"
//  @transient
//  val password = "123456"


  @transient
  val tablename = "upp:upp_user_profile"
  @transient
  def isManager(emp_id: String, emp_list: List[String],band :String): String = {
    if (emp_list.contains(emp_id) && !List("01","02","03","04","05","06","07").contains(band) && !("").equals(band+"") && !("null").equals(band.toLowerCase()) ) {
      return "Y"
    }
    else {
      return "N"
    }
  }

  //getDay function
  @transient
  def getDay(): String ={
    //select date_sub(’2012-12-08′,10)
    val df = new SimpleDateFormat("yyyy-MM-dd");
    return df.format(new Date());
  }

  //null2Str function
  @transient
  def null2Str(data: Any): String = {
    if (data != null || !"".equals(data)|| !"null".equals((data+"").toLowerCase))
      return data+""
    else
      return ""
  }

  //filterNotNull function
  @transient
  def filterNotNull(fister: String, second: String): String = {
    if (fister !=null && !"".equals(fister) && !"null".equals(fister.toLowerCase))
      return fister
    else if (second !=null && !"".equals(second) && !"null".equals(second.toLowerCase)) return second
    else return "NULL"

  }

  //getWorkingMonth(have new_user_label)
  @transient
  def getWorkingMonth(time :String,new_user_label: Int):String= {
    if (!"".equals(time) && time.length >= 8) {
      var cur_date = transferLongToDateStr(new Date().getTime)
      var month_space =getMonthSpace(time, cur_date)
      if (month_space > 0 && month_space <= new_user_label) {
        return "Y"
      }else{
        return "N"
      }
    }else{
      return "N"
    }
  }

  //filterNotNull function
  @transient
  def getWorkingYears(time: String): String = {
    if (!"".equals(time) && time.length >=8) {
      var cur_date = transferLongToDateStr(new Date().getTime)
      var month_space = getMonthSpace(time, cur_date)
      if (month_space > 0 && month_space <= 6) {
        return "0.5"
      } else if (month_space > 6 && month_space <= 12) {
        return "1"
      } else if(month_space > 12 && month_space <= 18){
        return "1.5"
      }else if (month_space > 18 && month_space <= 24) {
        return "2"
      }else if (month_space > 24 && month_space <= 30) {
        return "2.5"
      } else if (month_space > 30 && month_space <= 36) {
        return "3"
      } else if (month_space > 36 && month_space <= 42) {
        return "3.5"
      }else if (month_space > 42 && month_space <= 48) {
        return "4"
      }else if (month_space > 48 && month_space <= 54) {
        return "4.5"
      } else if (month_space > 54 && month_space <= 60) {
        return "5"
      } else if(month_space > 60){
        return "5+"
      }else{
        return "NULL"
      }
    }
    else {
      return "NULL"
    }
  }

  //transferLongToDateStr function
  @transient
  def transferLongToDateStr(millSec: Long): String = {
    val sdf = new SimpleDateFormat("yyyy-MM-dd")
    return sdf.format(new Date(millSec))
  }

  //getMonthSpace function
  @transient
  def getMonthSpace(befdate: String, curdate: String): Int = {
    val sdf = new SimpleDateFormat("yyyy-MM-dd")
    var result1 = 0
    var result2 = 0
    val c1 = Calendar.getInstance
    val c2 = Calendar.getInstance
    c1.setTime(sdf.parse(befdate))
    c2.setTime(sdf.parse(curdate))
    result1 = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)
    result2 = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH)
    return Math.abs(result2 + result1 * 12)+1
  }
  @transient
  def get_locale(country :String):String={
    var counry_time:Map[String,String] = Map()
    counry_time += ("Afghanistan" -> "GMT+4:30")
    counry_time += ("Argentina" -> "GMT-3")
    counry_time += ("Australia" -> "GMT+10")
    counry_time += ("Austria" -> "GMT+1")
    counry_time += ("Belgium" -> "GMT+1")
    counry_time += ("Brazil" -> "GMT-3")
    counry_time += ("Bulgaria" -> "GMT+2")
    counry_time += ("Canada" -> "GMT-5")
    counry_time += ("Chile" -> "GMT-4")
    counry_time += ("China" -> "GMT+8")
    counry_time += ("Chinese Taipei" -> "GMT+8")
    counry_time += ("Colombia" -> "GMT-5")
    counry_time += ("Croatia" -> "GMT+1")
    counry_time += ("Czech Republic" -> "GMT+1")
    counry_time += ("Denmark" -> "GMT+1")
    counry_time += ("Egypt" -> "GMT+2")
    counry_time += ("El Salvador" -> "GMT-6")
    counry_time += ("Finland" -> "GMT+2")
    counry_time += ("France" -> "GMT+1")
    counry_time += ("Georgia" -> "GMT-5")
    counry_time += ("Germany" -> "GMT+1")
    counry_time += ("Greece" -> "GMT+2")
    counry_time += ("Guatemala" -> "GMT-6")
    counry_time += ("Hong Kong China" -> "GMT+8")
    counry_time += ("Hungary" -> "GMT+1")
    counry_time += ("India" -> "GMT+5:30")
    counry_time += ("Indonesia" -> "GMT+7")
    counry_time += ("Ireland" -> "GMT+0")
    counry_time += ("Israel" -> "GMT+2")
    counry_time += ("Italy" -> "GMT+1")
    counry_time += ("Japan" -> "GMT+9")
    counry_time += ("Kazakhstan" -> "GMT+6")
    counry_time += ("Kenya" -> "GMT+3")
    counry_time += ("Lithuania" -> "GMT+2")
    counry_time += ("Luxembourg" -> "GMT+1")
    counry_time += ("Malaysia" -> "GMT+8")
    counry_time += ("Mexico" -> "GMT-7")
    counry_time += ("Morocco" -> "GMT+0")
    counry_time += ("Netherlands" -> "GMT+1")
    counry_time += ("New Zealand" -> "GMT+1")
    counry_time += ("Nigeria" -> "GMT+1")
    counry_time += ("Norway" -> "GMT+1")
    counry_time += ("Pakistan" -> "GMT+5")
    counry_time += ("Peru" -> "GMT-5")
    counry_time += ("Philippines" -> "GMT-8")
    counry_time += ("Poland" -> "GMT+1")
    counry_time += ("Portugal" -> "GMT+0")
    counry_time += ("Republic of Korea" -> "GMT+9")
    counry_time += ("Romania" -> "GMT+2")
    counry_time += ("Russian Federation" -> "GMT+3")
    counry_time += ("Saudi Arabia" -> "GMT+3")
    counry_time += ("Serbia" -> "GMT+1")
    counry_time += ("Singapore" -> "GMT+8")
    counry_time += ("Slovakia" -> "GMT+1")
    counry_time += ("Slovenia" -> "GMT+1")
    counry_time += ("South Africa" -> "GMT+2")
    counry_time += ("Spain" -> "GMT+1")
    counry_time += ("Sweden" -> "GMT+1")
    counry_time += ("Switzerland" -> "GMT+1")
    counry_time += ("Thailand" -> "GMT+7")
    counry_time += ("Tunisia" -> "GMT+1")
    counry_time += ("Turkey" -> "GMT+2")
    counry_time += ("Ukraine" -> "GMT+2")
    counry_time += ("The United Arab Emirates" -> "GMT+4")
    counry_time += ("United Kingdom" -> "GMT+0")
    counry_time += ("United States of America" -> "GMT-4")
    counry_time += ("Venezuela" -> "GMT-4:30")
    counry_time += ("Vietnam" -> "GMT+7")
    counry_time += ("Yugoslavia" -> "GMT+1")

    if (counry_time.contains(country)) return counry_time(country)
    else return "GMT+8"

    //    if(country==null ||  "".equals(country) || "null".equals(country.toLowerCase) || country.toLowerCase.contains("cn"))
    //    {
    //      return "GMT+8"
    //    }else if (country.toLowerCase.contains("us")){
    //      return "GMT-4"
    //    }else if (country.toLowerCase.contains("th")){
    //      return "GMT+7"
    //    }else if (country.toLowerCase.contains("vn")){
    //      return "GMT+7"
    //    }else if (country.toLowerCase.contains("ph")){
    //      return "GMT+8"
    //    }else if (country.toLowerCase.contains("hk")){
    //      return "GMT+8"
    //    }else if (country.toLowerCase.contains("pl")){
    //      return "GMT+1"
    //    }else if (country.toLowerCase.contains("ie")){
    //      return "GMT+1"
    //    }else if (country.toLowerCase.contains("bg")){
    //      return "GMT+2"
    //    }else if (country.toLowerCase.contains("ch")){
    //      return "GMT+1"
    //    }else if (country.toLowerCase.contains("sa")){
    //      return "GMT+3"
    //    }else if (country.toLowerCase.contains("ar")){
    //      return "GMT-3"
    //    }else{
    //      return "GMT+8"
    //    }
  }

}
